﻿using System;
using RpcClient.Attr;
using RpcModular.Domain;
using RpcModular.Model;

namespace RpcModular.Accredit
{
    /// <summary>
    /// 默认授权服务
    /// </summary>
    [ClassLifetimeAttr(ClassLifetimeType.单例)]
    internal class DefAccreditService : AccreditService, IAccreditService
    {
        public override void ClearAccredit()
        {
            _UserState = null;
            base.ClearAccredit();
        }

        public DefAccreditService() 
        {

        }
        [ThreadStatic]
        private static IUserState _UserState = null;

        private static readonly Type _StateType = typeof(UserState);
        public IUserState CurrentUser => _UserState;

        public override void SetCurrentAccredit(Guid accreditId)
        {
            base.SetCurrentAccredit(accreditId);
            _UserState = null;
        }
        public IUserState GetAccredit(Guid accreditId)
        {
            UserAccreditDomain accredit = _GetAccredit(accreditId);
            _UserState = accredit.GetUserState(_StateType);
            return _UserState;
        }
        public void CancelAccredit(Guid accreditId)
        {
            Cancel(accreditId);
        }
    }
}
